//
//  HelloWorldLayer.m
//  Racing
//
//  Created by vy phan on 4/12/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//

// Import the interfaces
#import "HelloWorldScene.h"
#define kHeroMovementAction 1
#define kPlayerSpeed 300

// HelloWorld implementation
@implementation HelloWorld

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	//HelloWorld *layer = [HelloWorld node];
	TestTrack *layer = [[TestTrack alloc] init];
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		
		// create and initialize a Label
		CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

		// ask director the the window size
		CGSize size = [[CCDirector sharedDirector] winSize];
	
		// position the label on the center of the screen
		label.position =  ccp( size.width /2 , size.height/2 );
		label.tag = 1;
		
		// add the label as a child to this Layer
		[self addChild: label];
		self.isAccelerometerEnabled = YES;
		[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];
		
		// test *.track file
		Track* track = [[Track alloc]init];
		NSString *filePath = [[NSBundle mainBundle] pathForResource:@"circle" ofType:@"track"];
		if (filePath) {
			NSLog(filePath);
			[track load:filePath];
		}
		else {
			printf(">>>>>>>>>>>>>>>>>>>> file not exits.\n");
		}

	}
	return self;
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    CGSize winsize = [[CCDirector sharedDirector] winSize];
	
	// grab the player sprite from that layer using it's tag
    CCSprite *playerSprite = (CCSprite *)[self getChildByTag:1];

	
	float destX, destY;
    BOOL shouldMove = NO;
	
    float currentX = playerSprite.position.x;
    float currentY = playerSprite.position.y;
	
    if(acceleration.x > 0.25) {  // tilting the device upwards
        destX = currentX - (acceleration.y * kPlayerSpeed);
        destY = currentY + (acceleration.x * kPlayerSpeed);
        shouldMove = YES;
    } else if (acceleration.x < -0.25) {  // tilting the device downwards
        destX = currentX - (acceleration.y * kPlayerSpeed);
        destY = currentY + (acceleration.x * kPlayerSpeed);
        shouldMove = YES;
    } else if(acceleration.y < -0.25) {  // tilting the device to the right
        destX = currentX - (acceleration.y * kPlayerSpeed);
        destY = currentY + (acceleration.x * kPlayerSpeed);
        shouldMove = YES;
    } else if (acceleration.y > 0.25) {  // tilting the device to the left
        destX = currentX - (acceleration.y * kPlayerSpeed);
        destY = currentY + (acceleration.x * kPlayerSpeed);
        shouldMove = YES;
    } else {
        destX = currentX;
        destY = currentY;
    }
	
    if(shouldMove) {
        CGSize wins = [[CCDirector sharedDirector] winSize];
        // ensure we aren't moving out of bounds     
        if(destX < 0 || destX > wins.width - 0 || destY < 0 || destY > wins.height - 0) {
            // do nothing
        } else {
            CCAction *action = [CCMoveTo actionWithDuration:1 position: CGPointMake(destX, destY)];
            [action setTag:kHeroMovementAction];
            [playerSprite runAction:action];
        }
    } else {
        // should stop
        [playerSprite stopActionByTag:kHeroMovementAction];
    }
	
	
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
